<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="查询时间" prop="daterangeNew">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="chart-container">
      <div id="barChart" style="width: 1000px; height: 400px;  margin-top: -20px; margin-bottom: 1px; "></div>
    </div>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:pretrial:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="numberList" @selection-change="handleSelectionChange"
      border
      stripe
      style="width: 100%"
      :header-cell-style="{background:'#f5f7fa',color:'#606266'}">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="orNuber" :min-width="130"  :width="130"  />
      <el-table-column label="地区" align="center" prop="departmentName"   >
        <template slot-scope="scope">
          <span>
            {{
              scope.row.departmentName === '本院' ? '黑龙江省院' :
              scope.row.departmentName === '黑龙江省院' ? '合计' :
              scope.row.departmentName
            }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="数量" align="center" prop="caseCusTotal">

      </el-table-column>
      <el-table-column label="同比" align="center" prop="peryear" >


      </el-table-column>
      <el-table-column label="占比" align="center" prop="perZhan" />
      <el-table-column label="占比增减" align="center" prop="peryearZhan" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listNumber } from "@/api/business/pretrial"
import * as echarts from 'echarts'

export default {
  name: "Pretrial",
  data() {
    const today = new Date().toISOString().split('T')[0]
    return {

      barChart: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 行政公益诉讼领域情况表格数据
      numberList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [today, today],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        daterangeNew: [
          { required: true, message: "查询时间年月不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (this.barChart) {
      this.barChart.dispose()
    }
  },
  methods: {
    /** 初始化图表 */
    initChart() {
      this.barChart = echarts.init(document.getElementById('barChart'))
      this.barChart.setOption({
        title: {
          text: '全省磋商数量统计',
          left: 'center',
          textStyle: {
              color: '#FFFFFF', // 设置为白色，以便在深色背景上更清晰
              fontSize: 22      // 增大字体大小，使其更醒目
        },
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: [],
          axisLabel: {
            interval: 0,
            rotate: 30, // 如果地区名太长可以旋转
            fontSize: 12, // 增大横坐标字体大小
            color: '#FFFFFF' // 设置为白色，以便在深色背景上更清晰
          }
        },
        yAxis: {
          type: 'value',
          name: '案件数量',
          nameTextStyle: {
          fontSize: 14, // 增大纵坐标字体大小
          color: '#FFFFFF' // 设置为白色，以便在深色背景上更清晰
          },
          axisLabel: {
            fontSize: 12, // 增大纵坐标字体大小
            color: '#FFFFFF' // 设置为白色，以便在深色背景上更清晰
          }
        },
        series: [{
          name: '案件数量',
          type: 'bar',
          data: [],
          itemStyle: {
            color: '#409EFF'
          },
          showBackground: true,
          backgroundStyle: {
            color: 'rgba(180, 180, 180, 0.2)'
          }
        }]
      })
    },
    /** 更新图表数据 */
    updateChart() {
      if (!this.barChart) return

      const areas = this.numberList.map(item => item.departmentName === '本院' ? '黑龙江省院' :
            item.departmentName === '黑龙江省院' ? '合计' : item.departmentName)
      const counts = this.numberList.map(item => item.caseCusTotal)

      this.barChart.setOption({
        xAxis: {
          data: areas
        },
        series: [{
          data: counts
        }]
      })
    },
    /**全省公益诉讼检察磋商情况统计表 */
    getList() {
      this.loading = true
      listNumber( this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.numberList = response.rows
        this.total = response.total
        this.loading = false
        this.updateChart() // 数据更新后刷新图表
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        area: null,
        casesTime: null,
        departmentName: null,
        caseNumber: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },



    /** 导出按钮操作 */
    handleExport() {
      this.download('system/processadmin/exportCus', {
        ...this.queryParams
      }, `processadminCushaha.xls`)
    }
  }
}
</script>
<style scoped>
.app-container {
  padding: 20px;
  width: 100%;
  box-sizing: border-box;
}

.table-title {
  font-size: 23px;
  font-weight: bold;
  color: #333;
  margin: 10px 0 20px 0; /* 减少上边距 */
  text-align: center;
}

.el-table {
  margin-top: 10px; /* 减少表格上边距 */
  width: 100% !important;
  table-layout: fixed !important; /* 关键修复点1 */
  border-collapse: collapse; /* 合并边框，使表头和表格的单元格边框连在一起 */
}
.el-table th,
.el-table td {
  padding: 8px 0 !important; /* 统一单元格内边距 */
  text-align: center !important;
  vertical-align: middle !important;
  border-right: 1px solid #ebeef5 !important; /* 确保边框一致 */
}

.el-table  th {
  background-color: #f5f7fa !important;
  font-size: 14px;
  white-space: nowrap;
  border-right: 1px solid #ebeef5 !important; /* 确保边框一致 */
}
.el-table /deep/ .cell {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0 10px !important; /* 调整内容间距 */
}


.el-table /deep/ .el-table__header {
  width: 100% !important;
}

.el-table /deep/ .el-table__body {
  width: 100% !important;
}

.el-form {
  margin-bottom: 10px;
}

.el-row.mb8 {
  margin-bottom: 10px !important;
}

/* 修复多级表头对齐问题 */
.el-table /deep/ .el-table__header .el-table-column--gutter {
  display: table-cell !important;
}

/* 添加表头下方的连接线样式 */
.el-table th:after {
  content: '';
  display: block;
  border-bottom: 1px solid #ebeef5;
  margin-top: -1px; /* 根据需要微调，确保连接线与表格内容紧贴 */
}
</style>
